(function(){

	if (!window.CJ) {
		window.CJ = {};
	}



	var EventPopupCtl = {
		init: function () {
			var i, len = this.entryPopup.arrPopup.length;
			if (len > 0) {
				for (i=0;i<len;i++) {
					this.entryPopup.arrPopup[i].del();
				}
			}
			this.entryPopup.showIndex = 0;
			this.transaction.arrData.length = 0;
			this.entryPopup.arrPopup.length = 0;
			this.entryPopup.arrAnswer.length = 0;
			this.history.arrStep.length = 0;
			this.transaction.arrData[0] = { userID: CJ.plugin.membership.getUserID() };
		},
		getEvent :function (type, id, succCB, backCB) {
			var self = this;
			var ajaxType, ajaxParam;


			//Event, Promotion, Poll 을 모두 동일하게 처리 할 수 있도록
			//데이터를 재구성
			this.INFO = {};
			this.INFO.type = type;//event, promotion, poll
			this.INFO.id = id;//event, promotion, poll

			this.backCallBack = backCB;
			if (type == 'event') {
				ajaxType = 'EVENTDETAIL';
				ajaxParam = {
					eventId: id
				};
			} else if (type == 'promotion') {
				ajaxType = 'PROMOTIONDETAIL';
				ajaxParam = {
					promotionID: id
				};
			} else {
				ajaxType = 'POLL';
				ajaxParam = {
					pollID: id
				};
			}
			EventPopupCtl.ajax(ajaxType, ajaxParam, function (json) {
				succCB();
				self.initSuccess(json);
			});
		},
		ajax : function (atype, aparam, succCB, errCB) {
			CJ.HttpJson.get({
				type: atype,
				param: aparam,
				success: function (json) {
					if (json.resultcode == "100") {
						succCB(json);
					} else {
						console.log(atype + ' ' + json.message);
						EventPopupCtl.backCallBack();
					}
				},
				error : function () {
					if (typeof errCB == 'function') errCB();
				}
			});
		},
		initSuccess : function (json) {
			this.json = json;
			this.init();

			//Event, Promotion, Poll 을 모두 동일하게 처리 할 수 있도록
			//데이터를 재구성
			if (this.INFO.type == 'poll') {
				this.INFO.eventType = "0";
				this.INFO.color = "#c9dfe9";
				this.INFO.quiz = json.pollquiz;
				EventPopupCtl.transaction.add({
					pollid:  EventPopupCtl.INFO.id
				});
			} else if (this.INFO.type == 'event') {
				this.INFO.eventType = json.eventtype;
				this.INFO.color = json.eventcolor;
				this.INFO.quiz = json.eventquiz;
				EventPopupCtl.transaction.add({
					eventid:  EventPopupCtl.INFO.id
				});
			} else {
				this.INFO.eventType = json.promotiontype;
				this.INFO.color = json.promotioncolor;
				this.INFO.quiz = json.promotionquiz;
				EventPopupCtl.transaction.add({
					promotionid:  EventPopupCtl.INFO.id
				});
			}

			EventPopupCtl.PopupView.init();
		},
		transaction : {
			arrData : [],
			add: function (data) {
				this.arrData.push(data);
			},
			remove : function () {
				this.arrData.pop();
			},
			commit : function () {
				var param, ajaxUrl, ajaxParam = {};
				
				if (EventPopupCtl.INFO.type == 'event') {
					ajaxType = 'EVENTINSERT';
				} else if (EventPopupCtl.INFO.type == 'promotion') {
					ajaxType = 'PROMOTIONINSERT';
				} else {
					ajaxType = 'POLLINSERT';
				}

				if (EventPopupCtl.INFO.eventType == "0" || EventPopupCtl.INFO.eventType == "1") {
					var answers = EventPopupCtl.entryPopup.arrAnswer.join('|');
					this.add({'answer' : answers});
				}

				for (var i=0;i<this.arrData.length;i++){
					param = this.arrData[i];
					$.extend(ajaxParam, param);
				}
				EventPopupCtl.ajax(ajaxType, ajaxParam, function (json) {
					//이벤트, 프로모션 완료 처리
					if (EventPopupCtl.INFO.type != 'poll') {
						EventPopupCtl.PopupComplete.show(EventPopupCtl.INFO.type, EventPopupCtl.INFO.color);
					//투표 완료 처리 > 결과 화면
					} else {
						EventPopupCtl.PollComplete.show(EventPopupCtl.INFO.id, EventPopupCtl.INFO.color);
					}
				});
			}
		},
		entryPopup : {
			showIndex : 0,
			arrPopup : [],
			arrAnswer : [],
			add : function (obj) {
				this.arrPopup.push(obj);
			},
			init : function () {
				this.arrPopup[0].show();
			},
			submit : function (quizanswer) {
				this.showIndex = this.showIndex + 1;
				//퀴즈 답변 추가
				this.arrAnswer.push(quizanswer);

				if (this.arrPopup.length > this.showIndex) {
					this.show(this.showIndex);
				} else if (this.arrPopup.length == this.showIndex) {
					//유저 정보 입력 이동
					if (EventPopupCtl.json.inputtype != '3') {
						EventPopupCtl.PopupUserInfo.show(EventPopupCtl.json.inputtype, EventPopupCtl.INFO.color);
					//유저 정보 입력 없이 완료
					} else {
						EventPopupCtl.transaction.commit();
					}
				}
			},
			cancel : function () {
				this.showIndex = this.showIndex - 1;
				if (this.showIndex >= 0) {
					this.show(this.showIndex);
					this.arrAnswer.pop();
				} else {
					EventPopupCtl.PopupView.show();
				}
			},
			show : function (index) {
				this.arrPopup[index].show();
			}
		},
		getJsonProp : function (prop) {
			var p = this.type + prop;
			return this.json[p];
		},
		entryEvent : function () {
			EventEntryHandler();
		},
		history : {
			arrStep : [],
			add : function (func) {
				this.arrStep.push(func);
			},
			back : function () {
				var func = this.arrStep.pop();
				if (typeof func == 'function') {
					func();
				}
			}
		},
		hideAll : function () {
			$('#popup_event>div.on').removeClass('on');
		}
	};

	//View Tyep 에 따른 내용 보기 Popup
	function PopupView () {
		this.name = 'PopupView';
		this.bindEvent();
	}
	PopupView.prototype = new EventCtl();
	PopupView.prototype.bindEvent = function () {
		var self = this;
		$('.confirmBtn').on('click', function () {
			self.enter();
		});
		$('.yesBtn').on('click', function () {
			self.enter();
		});
		$('.noBtn').on('click', function () {
			self.back();
		});
		$('.popup .btn_prev').on('click', function () {
			self.pageUp();
		});
		$('.popup .btn_next').on('click', function () {
			self.pageDown();
		});

		$('ul.btn>li, .popup .btn_prev, .popup .btn_next', $('#popup_event')).on('mouseenter', function () {
			self.mouseOver(this);
		});
	};
	PopupView.prototype.init = function () {
		var self = this;
		var json = EventPopupCtl.json;
		this.render();
		this.show();

		//텍스트 포함일 경우 텍스트 페이지 계산
		if (json.viewtype != "0") {
			this.pageInit();
		}
	};
	PopupView.prototype.show = function () {
		EventPopupCtl.hideAll();
		this.rootElem.addClass('on');
		CJ.Scene.setDelegate(this);
	};
	PopupView.prototype.render = function () {
		var json = EventPopupCtl.json;
		this.viewType = json.viewtype;
		//Popup Layer 선택
		if (json.viewtype == "0") { //이미지
			this.rootElem = $("#popup_view_image");
		} else if (json.viewtype == "1") { //이미지 + 텍스트
			this.rootElem = $("#popup_view_imagetext");
		} else { //텍스트
			this.rootElem =  $("#popup_view_text");
		}

		this.eImage = $('.eImage', this.rootElem);
		this.eText = $('.eText', this.rootElem);

		//텍스트 처리
		if (json.viewtype != "0") { //이미지 + 텍스트 or 텍스트
			this.eText.text(json.context);
		}
		//이미지 처리
		if (json.viewtype == "0") { //이미지
			this.eImage.attr('src', EventPopupCtl.getJsonProp('image'));
		}
		if (json.viewtype == "1") { //이미지 + 텍스트 
			this.eImage.attr('src', EventPopupCtl.getJsonProp('imiximage'));
		}

		this.btn = $('ul.btn>li', this.rootElem);
		this.confirmBtn = $('.confirmBtn', this.rootElem);
		this.yesBtn = $('.yesBtn', this.rootElem);
		this.noBtn = $('.noBtn', this.rootElem);
		this.btn_next = $('.btn_next', this.rootElem);
		this.btn_prev = $('.btn_prev', this.rootElem);

		// background-color 처리
		this.rootElem.css('background-color', EventPopupCtl.INFO.color);

		//eventtype 에 따른 버튼 처리 및 eventtype 과 viewtype 에 따른 Class 지정
		if (EventPopupCtl.INFO.eventType == "3") { //공지	3
			//eventtype 에 따른 버튼 처리
			this.confirmBtn.addClass("focus").show();
			this.yesBtn.removeClass("focus").hide();
			this.noBtn.removeClass("focus").hide();
			//eventtype 과 viewtype 에 따른 Class 지정
			if (json.viewtype == "0") { //이미지
				this.rootElem.removeClass().addClass('popup ly1');
			} else if (json.viewtype == "1") { //이미지 + 텍스트
				this.rootElem.removeClass().addClass('popup ly2');
			} else { //텍스트
				this.rootElem.removeClass().addClass('popup ly3');
			}
		} else { //객관식 0, 주관식 1, 단순참여 2
			//eventtype 에 따른 버튼 처리
			this.confirmBtn.removeClass("focus").hide();
			this.yesBtn.addClass("focus").show();
			this.noBtn.removeClass("focus").show();
			//eventtype 과 viewtype 에 따른 Class 지정
			if (json.viewtype == "0") { //이미지
				this.rootElem.removeClass().addClass('popup ly4');
			} else if (json.viewtype == "1") { //이미지 + 텍스트
				this.rootElem.removeClass().addClass('popup ly5');
			} else { //텍스트
				this.rootElem.removeClass().addClass('popup ly6');
			}
		}
	};
	PopupView.prototype.mouseOver = function (elem) {
		this.btn.removeClass('focus');
		this.btn_next.removeClass('focus');
		this.btn_prev.removeClass('focus');
		$(elem).addClass('focus');
	};
	PopupView.prototype.pageInit = function () {
		var textHeight = this.eText.height();
		this.wrapHeight = this.eText.parent().height();
		this.pageCount = Math.ceil(textHeight / this.wrapHeight);
		this.page = 0;
		this.eText.css('top', '0px');
		this.pageNum();
	};
	PopupView.prototype.pageUp = function () {
		if (0 <= this.page - 1) {
			this.page = this.page - 1;
			this.eText.css('top', '-' + (this.wrapHeight * this.page) + 'px');
			this.pageNum();
		}
	};
	PopupView.prototype.pageDown = function () {
		if (this.pageCount > this.page + 1) {
			this.page = this.page + 1;
			this.eText.css('top', '-' + (this.wrapHeight * this.page) + 'px');
			this.pageNum();
		}
	};
	PopupView.prototype.pageNum = function() {
		$('.btn_next>span, .btn_prev>span', this.rootElem).text(parseInt(this.page+1)+'/'+this.pageCount);
	};
	PopupView.prototype.up = function () {
		if (this.viewType != '0') {  //이미지 + 텍스트 or 텍스트
			this.btn.removeClass('focus');
			this.btn_next.removeClass('focus');
			this.btn_prev.addClass('focus');
		}
	};
	PopupView.prototype.down = function () {
		this.btn_next.removeClass('focus');
		this.btn_prev.removeClass('focus');
		if (EventPopupCtl.INFO.type != 'poll' && EventPopupCtl.INFO.eventType == "3") {
			this.confirmBtn.addClass("focus");
		} else {
			this.yesBtn.addClass("focus");
		}
	};
	PopupView.prototype.left = function () {
		if (this.yesBtn.hasClass('focus') || this.noBtn.hasClass('focus')) {
			this.yesBtn.addClass('focus');
			this.noBtn.removeClass('focus');
			return;
		}
		if (this.viewType != '0') {  //이미지 + 텍스트 or 텍스트
			this.btn.removeClass('focus');
			this.btn_next.removeClass('focus');
			this.btn_prev.addClass('focus');
		}
	};
	PopupView.prototype.right = function () {
		if (this.yesBtn.hasClass('focus') || this.noBtn.hasClass('focus')) {
			this.yesBtn.removeClass('focus');
			this.noBtn.addClass('focus');
			return;
		}
		if (this.viewType != '0') {  //이미지 + 텍스트 or 텍스트
			this.btn.removeClass('focus');
			this.btn_prev.removeClass('focus');
			this.btn_next.addClass('focus');
		}
	};
	PopupView.prototype.back = function () {
		this.rootElem.removeClass('on');
		EventPopupCtl.backCallBack();
	};
	PopupView.prototype.enter = function () {
		if (this.confirmBtn.hasClass('focus') || this.noBtn.hasClass('focus')) {
			this.back();
		} else if (this.btn_next.hasClass('focus')) {
			this.pageDown();
		} else if (this.btn_prev.hasClass('focus')) {
			this.pageUp();
		} else if (this.yesBtn.hasClass('focus')) {
			console.log('이벤트 참여');
			EventPopupCtl.entryEvent();
		}
	};

	//eventType
	// 객관식	0
	// 주관식	1
	// 단순참여	2
	// 공지		3

	//InputType
	// 연락처		0
	// 이름			1
	// 연락처+이름	2
	// 안받음		3
	//참여 처리기
	function EventEntryHandler () {
		var i, popObj;
		var json = EventPopupCtl.json;
		console.log('eventtype ' + EventPopupCtl.INFO.eventType + " inputtype " + json.inputType);
		//참여
		if (EventPopupCtl.INFO.eventType != '3') {


			if (EventPopupCtl.INFO.type == 'event' || EventPopupCtl.INFO.type == 'event') {
				EventPopupCtl.transaction.add({
					eventType: EventPopupCtl.INFO.eventType,
					InputType: json.inputType
				});
			}

			//객관식 or 주관식
			if (EventPopupCtl.INFO.eventType == '0' || EventPopupCtl.INFO.eventType == '1') {
				var quiz = EventPopupCtl.INFO.quiz;
				if (quiz.length > 0) {
					for (i=0;i<quiz.length;i++) {
						popObj = new EventEntry(EventPopupCtl.INFO.eventType, EventPopupCtl.INFO.color, quiz[i]);
						EventPopupCtl.entryPopup.add(popObj);
					}
					EventPopupCtl.entryPopup.init();
				} else {
					console.log("객관식 또는 주관식인데 문항이 없는 경우");
				}
			//단순참여
			} else {
				//유저 정보 입력 받음
				if (json.inputType != '3') {
					EventPopupCtl.PopupUserInfo.show(json.inputType);
				//안받음
				} else {
					EventPopupCtl.transaction.commit();
				}
			}
		//단순공지
		} else {
			//완료 팝업으로
			EventPopupCtl.PopupComplete.show(EventPopupCtl.INFO.type, EventPopupCtl.INFO.color);
		}
	}

	//eventType
	// 객관식	0
	// 주관식	1
	function EventEntry (type, color, quiz) {
		this.name = 'EventEntry';
		this.type = type;
		this.color = color;
		this.render(quiz);
		this.bindEvent();
	}
	EventEntry.prototype = new EventCtl();
	EventEntry.prototype.bindEvent = function () {
		var self = this;
		$('ol', this.rootElem).on('click', "li", function() {
			$(this).addClass('select focus').siblings().removeClass('select focus');
		});
		$('.quizSubmit', this.rootElem).on('click', function() {
			var isMouseClick = true;
			self.enter(isMouseClick);
		});
		$('.cancel', this.rootElem).on('click', function() {
			var isMouseClick = true;
			self.enter(isMouseClick);
		});
		$('li', this.rootElem).on('mouseenter', function() {
			$('li', self.rootElem).removeClass('focus');
			$(this).addClass('focus');
		});
	};
	EventEntry.prototype.render = function (quiz) {
		//객관식
		if (this.type == '0') {
			this.rootElem = $( $('#objectiveTemplate').text() );
			$('h5', this.rootElem).text(quiz.question);
			var li, docfrag = document.createDocumentFragment();
			for(var i=0;i<quiz.questionlist.length;i++) {
				li = document.createElement('li');
				li.innerHTML = '<span class="radio_box"></span>'+ quiz.questionlist[i].ex;
				if (i == 0) {
					li.setAttribute('class', 'select focus');
				}
				docfrag.appendChild(li);
			}
			$('ol', this.rootElem)[0].appendChild(docfrag);
		//주관식
		} else {
			this.rootElem = $( $('#subjectiveTemplate').text() );
			$('h5.tit', this.rootElem).text(quiz.question);
		}
		this.rootElem.css('background-color', this.color);
		$('#popup_event').append(this.rootElem);
	};
	EventEntry.prototype.show = function () {
		var self = this;
		EventPopupCtl.hideAll();
		this.rootElem.addClass('on');
		CJ.Scene.setDelegate(this);

		if (this.type == "1") {
			//LG 키보드 엔터 이벤트 처리기
			lgKb.enterInputField = function () {
				lgKb.focusOut();
			};

			InputFocusHandler = function () {
				self.focus();
			};

			//첫번째 input 에 자동 포커스
			var elem = $('input', this.rootElem);
			elem.focus();
			//LG 키보드 강제 포커스
			CJ.util.lgKbForceShow(elem.get(0));
		}
	};
	EventEntry.prototype.focus = function () {
		$('.focus', this.rootElem).removeClass('focus');
		$('.quizSubmit', this.rootElem).addClass('focus');
	};
	EventEntry.prototype.del = function () {
		this.rootElem.remove();
	};
	EventEntry.prototype.up = function () {
		var now;
		if (this.type == '0') {
			now = $('li.focus', this.rootElem);
			if (now.parent('ol').length > 0) {
				var prev = now.prev();
				if (prev.length>0) {
					now.removeClass('focus');
					prev.addClass('focus');
				}
			} else {
				now.removeClass('focus');
				$('ol>li:last-child', this.rootElem).addClass('focus');
			}
		} else {
			now = $('.focus', this.rootElem);
			if (now.hasClass('cancel')) {
				now.removeClass('focus');
				$('.quizSubmit', this.rootElem).addClass('focus');
			} else if (now.hasClass('quizSubmit')) {
				now.removeClass('focus');
				$('input', this.rootElem).addClass('focus');
			}
		}
	};
	EventEntry.prototype.down = function () {
		var now;
		if (this.type == '0') {
			now = $('li.focus', this.rootElem);
			if (now.parent('ol').length > 0) {
				var next = now.next();
				now.removeClass('focus');
				if (next.length>0) {
					next.addClass('focus');
				} else {
					$('li.quizSubmit', this.rootElem).addClass('focus');
				}
			}
		} else {
			now = $('.focus', this.rootElem);
			if (now.hasClass('input')) {
				now.removeClass('focus');
				$('.quizSubmit', this.rootElem).addClass('focus');
			} else if (now.hasClass('quizSubmit')) {
				now.removeClass('focus');
				$('.cancel', this.rootElem).addClass('focus');
			}
		}
	};
	EventEntry.prototype.left = function () {
		var now;
		if (this.type == '0') {
			now = $('li.focus', this.rootElem);
			if (now.parent('ul').length > 0) {
				var prev = now.prev();
				if (prev.length>0) {
					now.removeClass('focus');
					prev.addClass('focus');
				}
			}
		} else {
			now = $('.focus', this.rootElem);
			if (now.hasClass('cancel')) {
				now.removeClass('focus');
				$('.quizSubmit', this.rootElem).addClass('focus');
			} else if (now.hasClass('quizSubmit')) {
				now.removeClass('focus');
				$('input', this.rootElem).addClass('focus');
			}
		}
	};
	EventEntry.prototype.right = function () {
		var now;
		if (this.type == '0') {
			now = $('li.focus', this.rootElem);
			if (now.parent('ul').length > 0) {
				var next = now.next();
				if (next.length>0) {
					now.removeClass('focus');
					next.addClass('focus');
				}
			}
		} else {
			now = $('.focus', this.rootElem);
			if (now.hasClass('input')) {
				now.removeClass('focus');
				$('.quizSubmit', this.rootElem).addClass('focus');
			} else if (now.hasClass('quizSubmit')) {
				now.removeClass('focus');
				$('.cancel', this.rootElem).addClass('focus');
			}
		}
	};
	EventEntry.prototype.back = function () {
		EventPopupCtl.entryPopup.cancel();
	};
	EventEntry.prototype.enter = function (isMouseClick) {
		var now;
		//객관식
		if (this.type == '0') {
			now = $('li.focus', this.rootElem);
			//하단 버튼인 경우
			if (now.parent('ul').length > 0) {
				if (now.hasClass('quizSubmit')) {
					var answer = $('ol>.select', this.rootElem).index();
					EventPopupCtl.entryPopup.submit(answer);
				} else {
					this.back();
				}
			//퀴즈 항목인 경우
			} else {
				now.addClass('select').siblings().removeClass('select');
			}
		//주관식
		} else {
			now = $('.focus', this.rootElem);
			if (now.hasClass('input')) {
				now.removeClass('focus');
				now.focus();
			} else if (now.hasClass('quizSubmit')) {
				var answer = $('input', this.rootElem).val();
				if (answer != "") {
					EventPopupCtl.entryPopup.submit(answer);
				} else {
					console.log('주관식 답변을 쓰지 않은 경우 처리');
				}
			} else {
				this.back();
			}

			if (isMouseClick === true) {
				//키패드가 있는 상태에서 확인 버튼 클릭시 키패드 감춤
				setTimeout(function () {
					lgKb.focusOut();
				}, 100);
			}
		}
	};


	/*
	유저 정보 입력
	inputtype
	연락처			0
	이름			1
	연락처+이름		2
	안받음			3
	*/
	function PopupUserInfo () {
		this.name = 'PopupUserInfo';
		this.rootElem = $('#popup_entry_userinfo');
		this.userNameArea = $('#user_name');
		this.userPhoneArea = $('#user_phone');
		this.arrInputElem = $('input', this.rootElem);
		this.confirmBtn = $('ul.btn>li', this.rootElem);
		this.strID = '';
		this.arrInputID = [];
		this.bindEvent();
	}
	PopupUserInfo.prototype = new EventCtl();
	PopupUserInfo.prototype.bindEvent = function () {
		var self = this;
		$('li', this.rootElem).on('click', function () {
			var isMouseClick = true;
			self.enter(isMouseClick);
		});
		this.arrInputElem.on('focus', function() {
			self.strID = $(this).removeClass('focus').attr('id');
		});
		this.arrInputElem.on('click', function () {
			self.confirmBtn.removeClass('focus');
			self.strID = $(this).attr('id');
		});
	};

	PopupUserInfo.prototype.render = function (inputType, color) {
		this.rootElem.css('background-color', color);
		this.arrInputID.length = 0;
		if (inputType == "0") {//연락처
			this.userNameArea.hide();
			this.userPhoneArea.show();
			this.arrInputID.push("user_phone_input_1", "user_phone_input_2", "user_phone_input_3");
		} else if (inputType == "1") {//이름
			this.userNameAreashow();
			this.userPhoneArea.hide();
			this.arrInputID.push("user_name_input");
		} else if (inputType == "2") {//연락처+이름
			this.userNameArea.show();
			this.userPhoneArea.show();
			this.arrInputID.push("user_name_input", "user_phone_input_1", "user_phone_input_2", "user_phone_input_3");
		}
	};
	PopupUserInfo.prototype.show = function (inputType, color) {
		var self = this;
		
		this.arrInputElem.val('');
		this.render(inputType, color);
		EventPopupCtl.hideAll();
		this.rootElem.addClass('on');
		//$('.focus', this.rootElem).removeClass('focus');
		CJ.Scene.setDelegate(this);

		//LG 키보드 엔터 이벤트 처리기
		lgKb.enterInputField = function () {
			lgKb.focusOut();
		};

		InputFocusHandler = function () {
			self.focus();
		};

		//첫번째 input 에 자동 포커스

		this.strID = this.arrInputID[0];
		var elem = $('#'+this.strID);
		elem.focus();
		//LG 키보드 강제 포커스
		CJ.util.lgKbForceShow(elem.get(0));
	};
	PopupUserInfo.prototype.focus = function () {
		this.confirmBtn.removeClass('focus');
		this.arrInputElem.removeClass('focus');
		$('#'+this.strID).addClass('focus');
	};
	PopupUserInfo.prototype.up = function () {
		var nowIndex, nextIndex;
		var now = $('input.focus', this.rootElem);
		if (now.length > 0) {
			nowIndex = this.arrInputID.indexOf(now.attr('id'));
			nextIndex = nowIndex - 1;
			if (nextIndex >= 0) {
				this.strID = this.arrInputID[nextIndex];
				now.removeClass('focus');
				$('#'+this.strID).addClass('focus');
			}
		} else {
			this.confirmBtn.removeClass('focus');
			this.strID = this.arrInputID[this.arrInputID.length-1];
			$('#'+this.strID).addClass('focus');
		}
	};
	PopupUserInfo.prototype.left = function () {
		this.up();
	};
	PopupUserInfo.prototype.down = function () {
		var nowIndex, nextIndex;
		var now = $('input.focus', this.rootElem);
		if (now.length > 0) {
			now.removeClass('focus');
			nowIndex = this.arrInputID.indexOf(now.attr('id'));
			nextIndex = nowIndex + 1;
			if (nextIndex < this.arrInputID.length) {
				this.strID = this.arrInputID[nextIndex];
				$('#'+this.strID).addClass('focus');
			} else {
				this.confirmBtn.addClass('focus');
			}
		}
	};
	PopupUserInfo.prototype.right = function () {
		this.down();
	};
	PopupUserInfo.prototype.back = function () {
		EventPopupCtl.entryPopup.cancel();
	};
	PopupUserInfo.prototype.enter = function (isMouseClick) {
		var obj, val, uName, arrTel = [];
		if (isMouseClick === true || this.confirmBtn.hasClass('focus')) {
			for (var i=0;i<this.arrInputID.length;i++) {
				if (this.arrInputID[i] == 'user_name_input') {
					uName = $('#'+this.arrInputID[i]).val();
				} else {
					val = $('#'+this.arrInputID[i]).val();
					if (val != "") {
						arrTel.push(val);
					}
				}
			}
			if (uName == ""  && arrTel.length == 0) {
				console.log('입력 값이 없음에 대한 처리가 필요');
			} else {
				if (uName != "") {
					obj = {'userName': uName};
					EventPopupCtl.transaction.add(obj);
				}
				if (arrTel.length>0) {
					obj = {'userTel': arrTel.join('-')};
					EventPopupCtl.transaction.add(obj);
				}
				EventPopupCtl.transaction.commit();
			}

			if (isMouseClick === true) {
				//키패드가 있는 상태에서 확인 버튼 클릭시 키패드 감춤
				setTimeout(function () {
					lgKb.focusOut();
				}, 100);
			}
		} else {
			var input = $('input.focus', this.rootElem);
			input.removeClass('focus');
			input.focus();
		}
	};

	// 참여 완료 팝업
	function PopupComplete () {
		this.name = 'PopupComplete';
		this.rootElem = $('#popup_entry_complete');
		this.bindEvent();
	}
	PopupComplete.prototype = new EventCtl();
	PopupComplete.prototype.bindEvent = function () {
		var self = this;
		$('li.focus', this.rootElem).on('click', function(){
			self.back();
		});
	};
	PopupComplete.prototype.show = function (type, color) {
		this.render(type, color);

		EventPopupCtl.hideAll();
		this.rootElem.addClass('on');
		CJ.Scene.setDelegate(this);
	};
	PopupComplete.prototype.render = function (type, color) {
		var str =" 참여가 완료되었습니다.";
		if (type == 'event') {
			str = "이벤트" + str;
		} else if (type == 'promotion') {
			str = "프로모션" + str;
		} else {
			str = "투표" + str;
		}
		if (color) {
			this.rootElem.css('background-color', color);
		}
		$('h5.alert', this.rootElem).text(str);
	};
	PopupComplete.prototype.back = function () {
		EventPopupCtl.backCallBack();
	};
	PopupComplete.prototype.enter = function () {
		this.back();
	};


	// 투표 완료 팝업
	function PollComplete () {
		this.name = 'PollComplete';
		this.rootElem = $('#popup_entry_result');
		this.wrap = $('.result', this.rootElem);
		this.bindEvent();
	}
	PollComplete.prototype = new EventCtl();
	PollComplete.prototype.bindEvent = function () {
		var self = this;
		$('li.focus', this.rootElem).on('click', function(){
			self.back();
		});
	};
	PollComplete.prototype.show = function (id, color) {
		var self = this;
		CJ.HttpJson.get({
			type: 'POLLRESULT',
			param: {
				pollid : 1//id
			},
			success: function (json) {
				if (json.resultcode == "100") {
					self.success(json.pollquiz, color);
				} else {
					console.log('POLLRESULT' + json.messge);
				}
			},
			error : function () {
				console.log('POLLRESULT ajax 로드 에러');
			}
		});
	};
	PollComplete.prototype.success = function (quiz, color) {
		this.render(quiz, color);
		EventPopupCtl.hideAll();
		this.rootElem.addClass('on');
		CJ.Scene.setDelegate(this);
	};
	PollComplete.prototype.render = function (quiz, color) {
		var li, docfrag = document.createDocumentFragment();
		for(var i=0;i<quiz.length;i++) {
			li = document.createElement('li');
			li.innerHTML = quiz[i].viid +') ' + quiz[i].vititle + '<b>' +  CJ.util.numDot( quiz[i].votecount ) +' 명</b>';
			docfrag.appendChild(li);
		}
		this.wrap[0].innerHTML = '';
		this.wrap[0].appendChild(docfrag);
		this.rootElem.css('background-color', color);
	};
	PollComplete.prototype.back = function () {
		EventPopupCtl.backCallBack();
	};
	PollComplete.prototype.enter = function () {
		this.back();
	};


	function InputFocusHandler () {}
	//LG 키패드가 사라지면 검색 버튼에 자동 포커스 및 LG 키보드에서 input 에 컬러를 자동으로 바꿔줘야 하는데
	//처리가 안되는 것을 강제로 컬러를 지움
	function lgKbHide () {
		setTimeout(function(){
			$('#popup_event input').css('background-color', '');
			InputFocusHandler();
		}, 10);
	}

	$.extend(CJ.Scene, {
							popupView: function (type, id, succCB, backCB){
								EventPopupCtl.getEvent(type, id, succCB, backCB);
							}
						});

	$(document).ready(function(){
		EventPopupCtl.PopupView = new PopupView();
		EventPopupCtl.PopupUserInfo = new PopupUserInfo();
		EventPopupCtl.PopupComplete = new PopupComplete();
		EventPopupCtl.PollComplete = new PollComplete();

		//LG 키패드가 사라지면 input 에 컬러를 자동으로 바꿔줘야 하는데
		//처리가 안되는 것을 강제로 컬러를 지움
		LgKeyBoardHandler.addEventListener('hide', lgKbHide);
	});

})();